﻿using System;
using System.Data;
using System.Windows.Forms;


namespace CarBlackBox
{
    [Serializable]
    public class RecordInfoData
    {

        public const string ID_Field = @"ID";

        public const string RIN_Field = @"RIN";

        public const string PRIMARYKEY_Field = @"RIN";

        private Int32 _ID;

        private String _RIN;     //记录仪识别码

        public Int32 ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public String RIN
        {
            get { return _RIN; }
            set { _RIN = value; }
        }
        public RecordInfoData()
        {
            _ID = 0;
            _RIN = "";
        }
    }

    public class RecordInfoDataFactory
    {
        public static RecordInfoData Construct(IDataReader reader)
        {
            RecordInfoData data = new RecordInfoData();

            data.ID = Convert.ToInt32(reader["ID"]);

            data.RIN = Convert.ToString(reader["RIN"]);

            return data;
        }
        public static String GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static String GetPrimaryKey(DataTable dt, int rowIndex)
        {
            String PrimaryKey = null;

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { PrimaryKey = Convert.ToString(dr[RecordInfoData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static RecordInfoData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static RecordInfoData Construct(DataTable dt, int rowIndex)
        {
            RecordInfoData data = new RecordInfoData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.ID = Convert.ToInt32(dr["ID"]); }
            catch { }

            try { data.RIN = Convert.ToString(dr["RIN"]); }
            catch { }

            return data;
        }
        public static RecordInfoData Construct(DataGridView dgv, int rowIndex)
        {
            RecordInfoData data = new RecordInfoData();

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                switch (dgv.Columns[i].DataPropertyName)
                {
                    case "ID":
                        try { data.ID = Convert.ToInt32(dr[i].Value); }
                        catch { }
                        break;
                    case "RIN":
                        try { data.RIN = Convert.ToString(dr[i].Value); }
                        catch { }
                        break;
                }
            }
            return data;
        }
        public static string GetPrimaryKey(DataGridView dgv, int rowIndex)
        {
            string PrimaryKey = null;

            if (dgv.Rows.Count < rowIndex + 1)

                return null;

            DataGridViewCellCollection dr = dgv.Rows[rowIndex].Cells;

            if (dr == null)

                return null;

            for (int i = 0; i < dgv.Columns.Count; i++)
            {
                if (dgv.Columns[i].DataPropertyName == RecordInfoData.PRIMARYKEY_Field)
                {
                    try { PrimaryKey = Convert.ToString(dr[i].Value); }
                    catch { }
                    return PrimaryKey;
                }
            }
            return PrimaryKey;
        }
    }
}